package com.alogic.together2.service;

import com.alogic.together2.TogetherConstants;
import com.alogic.together2.TogetherServiceDescription;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.Script;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.doc.json.JsonObject;
import com.alogic.xscript.plugins.StringProcess;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import com.logicbus.backend.AbstractServant;
import com.logicbus.backend.Context;
import com.logicbus.backend.message.RawMessage;
import com.logicbus.backend.message.YamlMessage;
import com.logicbus.models.servant.ServiceDescription;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;

/**
 * 通过StringBuffer进行输入输出
 *
 * @since 1.6.14.13 [2021708 duanyy] <br>
 */
public class StringServant extends AbstractServant implements TogetherConstants {
    protected Script script = null;
    protected String service;
    protected String privilege;

    @Override
    protected void onDestroy() {

    }

    @Override
    protected void onCreate(ServiceDescription sd) {
        service = sd.getPath();
        privilege = sd.getPrivilege();

        if (sd instanceof TogetherServiceDescription){
            script = ((TogetherServiceDescription)sd).getScript();
        }else{
            Properties p = sd.getProperties();
            String bootstrap = PropertiesConstants.getString(p,BOOTSTRAP,EMPTY,true);
            if (StringUtils.isEmpty(bootstrap)){
                String config = PropertiesConstants.getString(p,SCRIPT,EMPTY);
                if (StringUtils.isNotEmpty(config)){
                    script = Script.create(config, p);
                }
            }else{
                String config = PropertiesConstants.getString(p,SCRIPT,EMPTY);
                if (StringUtils.isNotEmpty(config)){
                    script = Script.create(bootstrap, config, p);
                }
            }
        }
    }

    @Override
    protected int onJson(Context ctx)  {
        if (script != null){
            RawMessage msg = (RawMessage) ctx.asMessage(RawMessage.class);

            LogicletContext logicletContext = new Context.ServantLogicletContext(ctx);
            logicletContext.setObject(ID_CONTEXT, ctx);
            logicletContext.SetValue(ID_SERVICE, service);
            logicletContext.SetValue(ID_PRIVILEGE,StringUtils.isEmpty(privilege)?service:privilege);
            logicletContext.setObject(StringProcess.DFT_CONTEXT_ID,msg.getBuffer());
            try {
                XsObject doc = new JsonObject(ROOT,new HashMap<String, Object>());
                script.execute(doc,doc, logicletContext, null);
            }finally{
                logicletContext.removeObject(ID_CONTEXT);

                String keyword = logicletContext.GetValue(ID_KEYWORD, EMPTY);
                if (StringUtils.isNotEmpty(keyword)){
                    ctx.setKeyword(keyword);
                }

                ctx.setStatus(PropertiesConstants.getInt(logicletContext,ID_STATUS,Context.DFT_STATUS));
            }
        }else{
            ctx.asMessage(RawMessage.class);
        }
        return 0;
    }

    @Override
    protected int onYaml(Context ctx)  {
        if (script != null){
            YamlMessage msg = (YamlMessage) ctx.asMessage(YamlMessage.class);

            LogicletContext logicletContext = new Context.ServantLogicletContext(ctx);
            logicletContext.setObject(ID_CONTEXT, ctx);
            logicletContext.SetValue(ID_SERVICE, service);
            logicletContext.SetValue(ID_PRIVILEGE,StringUtils.isEmpty(privilege)?service:privilege);
            try {
                XsObject doc = new JsonObject(ROOT,msg.getRoot());
                script.execute(doc,doc, logicletContext, null);
            }finally{
                logicletContext.removeObject(ID_CONTEXT);

                String keyword = logicletContext.GetValue(ID_KEYWORD, EMPTY);
                if (StringUtils.isNotEmpty(keyword)){
                    ctx.setKeyword(keyword);
                }

                String outputPath = logicletContext.GetValue(ID_OUTPATH, EMPTY);
                if (StringUtils.isNotEmpty(outputPath)){
                    msg.setOutputPath(outputPath);
                }

                ctx.setStatus(PropertiesConstants.getInt(logicletContext,ID_STATUS,Context.DFT_STATUS));
            }
        }else{
            ctx.asMessage(YamlMessage.class);
        }
        return 0;
    }
}